<?php

namespace app\home\controller;

use app\company\service\DataService;
use library\Controller;
use think\Db;

/**
 * 仓库订单列表管理
 * Class User
 * @package app\worker\controller
 */
class WareHouseOrder extends Controller
{

    /**
     * 绑定当前数据表
     * @var string
     */
    protected $table = 'WareHouse';

    /**
     * 仓库订单列表管理
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '仓库订单管理';
        $query = $this->_query($this->table)
        ->alias('wh')
        ->join('platform p', 'p.id = wh.platform_id', 'left')
        ->field('wh.*,p.name as platform_name')
        ->dateBetween('wh.create_at#create_at')
        ->where(['wh.is_deleted' => '0', 'wh.status' => 1]);
        if(session('user.ware_house_id')){
            $query->where('wh.id', session('user.ware_house_id')); 
        }
        $query->like('wh.name#name')
        ->equal('wh.status#status')
        ->order('wh.sort desc, wh.create_at desc')
        ->page();
    }

    protected function _index_page_filter(&$data)
    {
        foreach($data as &$v){
            $v['complete_count'] = 0;//完成件数
            $v['sum_price'] = 0;//总价
            $plan_detail_user = Db::name('plan_detail_user')
                ->alias('pdu')
                ->join('plan_detail pd', 'pd.id = pdu.plan_detail_id', 'left')
                ->join('plan p', 'p.id = pd.plan_id', 'left')
                ->where('p.ware_house_id', $v['id'])
                ->where('p.is_deleted', 0)
                ->where('p.status', 1)
                ->field('pdu.complete_count,pd.price')
                ->select();
            $v['plan_detail_user'] = $plan_detail_user;
            foreach($plan_detail_user as $ko=>$vo){
                $v['complete_count'] += $vo['complete_count'];
                $v['sum_price'] += $vo['complete_count'] * $vo['price'];
            }
            //查询新增运营计划数量
            $v['is_new_count'] = Db::name('Plan')
                ->where('is_deleted', 0)
                ->where('status', 1)
                ->where('is_new', 1)
                ->where('ware_house_id', $v['id'])
                ->count();
        }
    }

    /**
     * 运营计划列表
     * @auth true
     */
    public function plan_index()
    {}

}
